<?php
/*
* This file is part of the Geek-Zoo Projects.
*
* @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
* @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
* @author xuanyan <xuanyan@geek-zoo.com>
*
*/

require_once ROOT_PATH.'/Abstract/front_abstract.php';

class Action extends front_abstract
{
    function index()
    {
        $acced = $this->acced();    
        $this->smarty->assign('acced', $acced);
        $this->smarty->display('permission.html');
    }

    function modify(){
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $left = _POST('left', array());
            $right = _POST('right', array());
            if($left && $right){
                foreach($right as $val){
                    $lee = $left;//赋值lee防止unset后 left丢失数据
                    $racc = _model('user')->read(array('account_id'=>$val));
                    foreach($lee as $key=>$var){
                        if($var == $val)
                            unset($lee[$key]);
                    }
                    if($racc['accounts']){
                        $join = explode(',', $racc['accounts']);
                        $lee = array_unique(array_merge($join, $lee));
                    }
                    $accounts = join(',', $lee);
                    !_model('user')->update(array('account_id'=>$val), array('accounts'=>$accounts));
                }
                $this->redirect('permission.html');
            }else{
                $this->redirect('permission.html');
            }
        }else{
            $acced = $this->acced();
        }
        $this->smarty->assign('acced', $acced);
        $this->smarty->display('lib/addper.html');
    }

    function edit()
    {
        if($_SERVER['REQUEST_METHOD'] == 'POST') {
            $left = _POST('left', array());
            $rid = _POST('right');
            if($rid){
                if($left){
                    _model('user')->update(array('account_id'=>$rid), array('accounts'=>implode(',', $left)));
                }else{
                    _model('user')->update(array('account_id'=>$rid), array('accounts'=>''));
                }
                $this->redirect('permission.html');
            }
        }else{
            $rid = _GET('id');
            if(!$rid){
                return 0;
            }
            $info = _model('user')->read(array('account_id' => $rid));
			
            if ($info) {
                $info['accounts'] = explode(',', $info['accounts']);
            }
            $this->smarty->assign('info', $info);
            $this->smarty->assign('acced', $this->acced());
            $this->smarty->display('lib/editper.html');
        }
    }
            
    function acced(){//获得主账号成员
        $acced = array();
        if ($_SESSION['user']['parent_id']) {
            return array();
        }
        $info = _model('user')->getList(array(
            'parent_id' => $_SESSION['user']['id']
        ));
        foreach($info as $key => $val){
            $scname = array();
            $tto = _model('account')->read(array('id' => "{$val['account_id']}"));
            $cc = $this->api->getUserInfo($tto['oauth_token'], $tto['oauth_token_secret']);
            $acced[$key]['id'] = $val['account_id'];
            $acced[$key]['name'] = $cc['screen_name'];
            if( !$val['accounts'] ){
                $acced[$key]['acced'] = '无分配用户';
                continue;
            }
            $tto = _model('account')->getList("WHERE id IN ({$val['accounts']})");
            foreach($tto as $var){
                $cc = $this->api->getUserInfo($var['oauth_token'], $var['oauth_token_secret']);
                $scname[] = $cc['screen_name'];
            }
            $acced[$key]['acced'] = join('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $scname);
            unset($scname);
        }

        return $acced;
    }

	function delete()
	{
        $rid = _GET('id');
        if(!$rid){
            return 0;
        }
        _model('user')->update(array('account_id'=>$rid), array('accounts'=>''));
        $this->redirect('permission.html');
	}
}